#!/usr/bin/env bash -e

version=$1
if [ -z $version ]; then
  currentversion=`scripts/version`
  echo "Enter release version (current is $currentversion):"
  read version
fi
export DIST_VERSION=$version

files="component.json package.json bower.json"
echo "Releasing with version $version"

echo "1. Pulling from origin"
git pull --ff-only --no-rebase origin

echo "2. Building and testing"
npm ci
npm test

echo "3. Setting version info to descriptor files"
sed -i "" 's/\("version".*:.*\)".*"/\1"'$version'"/' $files

echo "4. Commit and tag"
git add .
git commit -m "release $version"
git tag $version

echo "5. Push to origin/master"
git push
git push --tags origin

echo "6. Publish to npm --tag latest"
npm publish

echo "DONE! Released version $version"
